package marcus.age.impl;

import java.util.Date;

import marcus.age.AgeFormatter;
import marcus.jpa.entities.Contact;

import org.joda.time.Period;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;

public class ShortAgeFormatter implements AgeFormatter {

	public String format(Contact contact, Period period, Date date) {

		int year;
		int month;
		if (period==null)
			return "Not yet born";
		if (period.getDays()>15){
			if (period.getMonths()==11){
				month = 0;
				year = period.getYears() + 1;
			}
			else {
				month = period.getMonths() +1;
				year = period.getYears();
			}
		}
		else {
			month = period.getMonths();
			year = period.getYears();
		}
		
		Period p = new Period();
		p = p.plusMonths(month);
		p = p.plusYears(year);
		if (month == 0 && year == 0){
			p.plusDays(period.getDays());
		}
		PeriodFormatter formatter = new PeriodFormatterBuilder()
		.appendYears()
		.appendSuffix(" yr ", " yrs ")
        .appendMonths()
        .appendSuffix(" mth ", " mths ")
        .appendDays()
        .appendSuffix(" day ", " days ")
        .toFormatter();
		
		return formatter.print(p);
	}

}
